package com.zn.kjj.util.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;




/**
 * @ClassName: OnlineUsers
 * @Description: 在线用户统计
 * @author w15579
 * @date. 2016年12月15日
 * @version v1.0.9
 */
public class SessionSupport implements HttpSessionBindingListener , Serializable{
    /**
     * @Fields serialVersionUID : .
     */
    private static final long serialVersionUID = -3811751160129403624L;
    /**
     * log
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SessionSupport.class);
    /**
     * 对象实例(即OnlineUserListener的实例)作为一个属性被设置到session的
     * 时候，会调用本方法，这种情况一般发生在点击登录按钮以后的处理过程中 设置
     * @param event event
     * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
     */
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        LOGGER.info("Session ID is added to the list: {} Is beginning to!", event.getSession().getId());
        String strSid = event.getSession().getId();
        if(event.getSession().getId().contains(".")){
            strSid = strSid.split("\\.")[0];
        }

    }
    /**
     * 当Session超时，或本实例被从session中移除的时候被调用，这种情况一般 发生在注销方法的处理过程中
     * @param event event
     * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
     */
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        try {
            LOGGER.info("Session ID is removed to the list: {} Is beginning to!", event.getSession().getId());
            HttpSession session = event.getSession();
            String strSid = session.getId();
            if(event.getSession().getId().contains(".")){
                strSid = strSid.split("\\.")[0];
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
